package com.ycy.chuwenuniapp.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ycy.chuwenuniapp.common.ResCodeEnum;
import com.ycy.chuwenuniapp.common.ResData;
import com.ycy.chuwenuniapp.dto.GoodsOutboundRecordDto;
import com.ycy.chuwenuniapp.entity.AdminKey;
import com.ycy.chuwenuniapp.entity.Goods;
import com.ycy.chuwenuniapp.entity.GoodsOutboundRecord;
import com.ycy.chuwenuniapp.entity.User;
import com.ycy.chuwenuniapp.exception.CustomException;
import com.ycy.chuwenuniapp.mapper.IAdminKeyMapper;
import com.ycy.chuwenuniapp.mapper.IGoodsOutboundRecordMapper;
import com.ycy.chuwenuniapp.vo.GoodsOutboundRecordVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsOutboundRecordService extends ServiceImpl<IGoodsOutboundRecordMapper, GoodsOutboundRecord> {
    @Resource
    private UserService userService;

    //增加或修改
    public ResData<Boolean> saveRecord(GoodsOutboundRecord entity) {
        String userId = entity.getUserId();
        String typeName = entity.getTypeName();
        String name = entity.getName();
        Double price = entity.getPrice();
        String orderListJsonStr = entity.getOrderListJsonStr();
        if(price == null) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失price");
        if(StrUtil.isBlank(userId)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失userId");
        if(StrUtil.isBlank(typeName)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失typeName");
        if(StrUtil.isBlank(name)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失name");
        if(StrUtil.isBlank(orderListJsonStr)) throw new CustomException(ResCodeEnum.BAD_REQUEST,"缺失orderListJsonStr");

        return ResData.success(saveOrUpdate(entity));
    }

    //查全部
    public ResData<List<GoodsOutboundRecordVo>> findAll(GoodsOutboundRecordDto dto) {
        String typeName = dto.getTypeName();
        String name = dto.getName();
        String beginTime = dto.getBeginTime();
        String endTime = dto.getEndTime();
        QueryWrapper<GoodsOutboundRecord> wrapper = new QueryWrapper<>();
        wrapper.like(StrUtil.isNotBlank(typeName), "type_name",typeName);
        wrapper.like(StrUtil.isNotBlank(name), "name",name);
        wrapper.between(StrUtil.isNotBlank(beginTime) && StrUtil.isNotBlank(endTime), "create_time", beginTime, endTime);
        List<GoodsOutboundRecord> list = list(wrapper);
        if(list.size() == 0) return ResData.success(new ArrayList<>());

        ArrayList<String> userIdList = new ArrayList<>();
        list.forEach(item -> userIdList.add(item.getUserId()));
        QueryWrapper<User> userWrapper = new QueryWrapper<>();
        userWrapper.in("id", userIdList);
        userWrapper.select("id","name");
        List<User> userList = userService.list(userWrapper);

        ArrayList<GoodsOutboundRecordVo> voList = new ArrayList<>();
        for (GoodsOutboundRecord entity: list) {
            for (User user: userList) {
                if (entity.getUserId().equals(user.getId())) {
                    GoodsOutboundRecordVo vo = new GoodsOutboundRecordVo();
                    BeanUtil.copyProperties(entity,vo);
                    vo.setUserName(user.getName());
                    voList.add(vo);
                    break;
                }
            }
        }
        return ResData.success(voList);
    }
}
